package cinema.v1_1.service;

import cinema.v1_1.model.*;
import cinema.v1_1.util.FileUtil;

import java.util.*;
import java.util.stream.Collectors;

public class DataStore {

    private static final String USER_FILE    = "data/users.txt";
    private static final String MOVIE_FILE   = "data/movies.txt";
    private static final String HALL_FILE    = "data/halls.txt";
    private static final String SESSION_FILE = "data/sessions.txt";

    /* ---------------- User ---------------- */
    public static List<User> loadUsers() {
        List<User> list = new ArrayList<>();
        try {
            String data = FileUtil.read(USER_FILE);
            for (String line : data.split("\n")) {
                if (line.trim().isEmpty()) continue;
                String[] p = line.split(",");
                list.add(new User(p[0], p[1], p[2], p[3], p[4], p[5]));
            }
        } catch (Exception ignored) {}
        return list;
    }

    public static void saveUsers(List<User> list) {
        String data = list.stream()
                .map(u -> String.join(",", u.getId(), u.getName(), u.getPwdMd5(),
                        u.getType(), u.getPhone(), u.getRegTime()))
                .collect(Collectors.joining("\n"));
        try { FileUtil.write(USER_FILE, data); } catch (Exception e) { e.printStackTrace(); }
    }

    /* ---------------- Movie ---------------- */
    public static List<Movie> loadMovies() {
        List<Movie> list = new ArrayList<>();
        try {
            String data = FileUtil.read(MOVIE_FILE);
            for (String line : data.split("\n")) {
                if (line.trim().isEmpty()) continue;
                String[] p = line.split(",");
                list.add(new Movie(p[0], p[1], p[2], p[3], p[4],
                        Integer.parseInt(p[5]), Double.parseDouble(p[6])));
            }
        } catch (Exception ignored) {}
        return list;
    }

    public static void saveMovies(List<Movie> list) {
        String data = list.stream()
                .map(m -> String.join(",", m.getId(), m.getName(), m.getDirector(),
                        m.getActors(), m.getPlot(),
                        String.valueOf(m.getDuration()),
                        String.valueOf(m.getScore())))
                .collect(Collectors.joining("\n"));
        try { FileUtil.write(MOVIE_FILE, data); } catch (Exception e) { e.printStackTrace(); }
    }

    /* ---------------- Hall ---------------- */
    public static List<Hall> loadHalls() {
        List<Hall> list = new ArrayList<>();
        try {
            String data = FileUtil.read(HALL_FILE);
            for (String line : data.split("\n")) {
                if (line.trim().isEmpty()) continue;
                String[] p = line.split(",");
                list.add(new Hall(p[0], p[1], Integer.parseInt(p[2]), Integer.parseInt(p[3])));
            }
        } catch (Exception ignored) {}
        return list;
    }

    public static void saveHalls(List<Hall> list) {
        String data = list.stream()
                .map(h -> String.join(",", h.getId(), h.getName(),
                        String.valueOf(h.getRows()), String.valueOf(h.getCols())))
                .collect(Collectors.joining("\n"));
        try { FileUtil.write(HALL_FILE, data); } catch (Exception e) { e.printStackTrace(); }
    }

    /* ---------------- Session ---------------- */
    public static List<Session> loadSessions() {
        List<Session> list = new ArrayList<>();
        try {
            String data = FileUtil.read(SESSION_FILE);
            for (String line : data.split("\n")) {
                if (line.trim().isEmpty()) continue;
                String[] p = line.split(",");
                list.add(new Session(p[0], p[1], p[2], p[3], Double.parseDouble(p[4])));
            }
        } catch (Exception ignored) {}
        return list;
    }

    public static void saveSessions(List<Session> list) {
        String data = list.stream()
                .map(s -> String.join(",", s.getId(), s.getMovieId(), s.getHallId(),
                        s.getStartTime(), String.valueOf(s.getPrice())))
                .collect(Collectors.joining("\n"));
        try { FileUtil.write(SESSION_FILE, data); } catch (Exception e) { e.printStackTrace(); }
    }
}